﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using ApiBetaSeries.Model;
using ApiBetaSeries.Factories;

namespace ApiBetaSeries.Model
{
    /// <summary>
    /// Represent a Season of a show
    /// </summary>
    internal class Season : ApiBetaSeries.ISeason
    {
        /// <summary>
        /// Empty constructor
        /// </summary>
        public Season() { }

        #region Attributes
        /// <summary>
        /// Url of the show
        /// </summary>
        public string UrlShow { get; set; }

        /// <summary>
        /// Season number
        /// </summary>
        public int Number { get; set; }
        /// <summary>
        /// List of the episode of the season
        /// </summary>
        public List<IEpisode> Episodes { get; set; }

        /// <summary>
        /// Allow to access to an episode of this season
        /// </summary>
        /// <param name="number">number of the episode</param>
        /// <returns>The episode</returns>
        public IEpisode this[int number]
        {
            get { return Episodes[number]; }
            set { Episodes[number] = value; }
        }
        #endregion Atrributes

        #region inherits
        public override bool Equals(object obj)
        {
            if (obj == null) return false;

            if (this.GetType() != obj.GetType()) return false;

            Season s = (Season)obj;
            if (!Object.Equals(this.UrlShow, s.UrlShow)) return false;
            if (!Object.Equals(this.Number, s.Number)) return false;

            if (this.Episodes == null)
            {
                if (s.Episodes != null) return false;
                else return true;

            }

            if (this.Episodes.Count != s.Episodes.Count) return false;
            for (int i = 0; i < this.Episodes.Count; ++i)
            {
                if (!Object.Equals(this.Episodes[i], s.Episodes[i])) return false;
            }

            return true;
        }

        public override int GetHashCode()
        {
            return UrlShow.GetHashCode() ^ Number.GetHashCode();
        }

        public override string ToString()
        {
            return UrlShow + "  season : " + Number + " - (" + (Episodes != null ? Episodes.Count : 0) + " episodes)";
        }
        #endregion inherits

        #region Methods
        /// <summary>
        /// Get all the subtitles for the season
        /// </summary>
        /// <param name="type">VO/VF/VOVF</param>
        /// <returns>List of subtitles</returns>
        [ApiBetaSeries.Version.ApiMethod(true, "subtitles/show")]
        public List<ISubtitle> GetSubtitles(SubtitleLanguage type = SubtitleLanguage.VOVF)
        {
            List<ISubtitle> list = new List<ISubtitle>();
            if (type == SubtitleLanguage.Other)
            {
                throw new ArgumentException("Cannot use SubtitleLanguage 'Other' for this request");
            }
            string stype = type.ToString();

            XmlDocument doc = Utils.UtilsXML.GetUrl("/subtitles/show/" + this.UrlShow + ".xml", "&season=" + this.Number + "&language=" + stype);
            XmlElement xe = doc["root"]["subtitles"];

            if (xe == null)
            {
                throw new BetaSeriesException(1003, "Invalid action.");
            }
            foreach (XmlElement sub in xe.GetElementsByTagName("subtitle"))
            {
                ISubtitle s = SubtitleFactory.CreateSubtitle(sub);
                list.Add(s);
            }

            return list;
        }
        #endregion Methods
    }
}
